package demo4_1.controller;

import demo4_1.mapper.IUserMapper;
import demo4_1.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Scanner;
@Component
public class UserController {
    @Autowired
    private IUserMapper userMapper;
    public void start(){
        System.out.println("请选择对应的操作,输入序号");
        System.out.println("1.注册,2.登录,3.修改用户信息,4.用户列表,5.删除用户,6.再说");
        Scanner scanner = new Scanner(System.in);
        int num = Integer.parseInt(scanner.nextLine());
        switch (num){
            case 1:
                regUser();
                break;
            case 2:
                loginUser();
                break;
            case 3:
                updateUser();
                break;
            case 4:
                listUser();
                break;
            case 5:
                deleteByUsername();
                break;
            default:
                System.out.println("其他功能还在完善,目前404");
                break;
        }
    }
    //1.注册
    private void regUser() {
        System.out.println("欢迎用户注册");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = scanner.nextLine();

            //判断是否为重复用户
            if (userMapper.selectByUsername(username)!=null ) {
                System.out.println("该用户已存在!");

            }else {
                System.out.println("请输入密码");
                String password = scanner.nextLine();
                System.out.println("请输入昵称");
                String nickname = scanner.nextLine();
                System.out.println("请输入年龄");
                int age =scanner.nextInt();
                User user = new User(null, username, password, nickname, age);
                int num = userMapper.insert(user);
                if (num > 0) {
                    System.out.println("注册成功");
                } else {
                    System.out.println("注册失败");
                }
            }

    }
//2.登录
    private void loginUser() {
        System.out.println("欢迎用户登录");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        User user = userMapper.selectByUsername(username);
        if (user!=null && user.getPassword().equals(password)){
            System.out.println("登录成功,欢迎"+user.getNickname()+"回来");
            System.out.println(user);
        }else {
            System.out.println("登录失败");
        }

    }
//3.修改用户信息
    private void updateUser() {
        System.out.println("欢迎用户修改信息");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改的用户名");
        String username = scanner.nextLine();
        User user = userMapper.selectByUsername(username);
        if (user==null){
            System.out.println("用户不存在");
        }else {
            System.out.println("请输入新密码");
            String password = scanner.nextLine();
            System.out.println("请输入新昵称");
            String nickname = scanner.nextLine();
            System.out.println("请输入新年龄");
            int age = scanner.nextInt();
            user.setPassword(password);
            user.setNickname(nickname);
            user.setAge(age);
            int num = userMapper.update(user);
            System.out.println(num>0?"修改成功":"修改失败");

        }
    }
//4.用户列表
    private void listUser() {
        System.out.println("用户列表");
        for (User user : userMapper.selectAll()) {
            System.out.println(user);
        }
    }

    //5 删除用户
    private void deleteByUsername() {
        System.out.println("删除用户");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的用户名");
        String username = scanner.nextLine();
        int num = userMapper.deleteByUsername(username);
        System.out.println(num>0?"删除成功":"删除失败");
    }
}
